package com.sp.pay.action.user;

import com.sp.pay.common.Tool;
import com.sp.pay.dto.QueryDto;
import com.sp.pay.dto.user.UserDto;
import com.sp.pay.repository.user.UserRepository;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping(value = "user/")
public class UserAction {

	private  String INDEX = "user/index";
    private  String DETAIL = "user/detail";
    private  String ADD = "user/add";
    private  String EDIT = "user/edit";
	private  String LOGIN = "login";
	
	private static Log logger = LogFactory.getLog(UserAction.class);
	
    @Autowired
    private UserRepository userRepository;
	
	@RequestMapping(value ="index.do" )
	public String index(Model model){
        QueryDto<UserDto> queryDto = new QueryDto<UserDto>(new UserDto(),0,10);
        model.addAttribute("result",userRepository.query(queryDto));
		return INDEX;
	}

    @RequestMapping(value ="detail/{id}.do" )
    public String detail(@PathVariable("id") int id, Model model){
        model.addAttribute("user",userRepository.findById(id));
        return DETAIL;
    }

    @RequestMapping(value ="newUser.do" )
    public String newUser(){
        return ADD;
    }

    @RequestMapping(value ="{id}/editUser.do" )
    public String editUser(Model model,@PathVariable("id") int id){
        model.addAttribute("user",userRepository.findById(id));
        return EDIT;
    }

    @RequestMapping(value ="delUser.do" )
    public String delUser(Model model, int id){
    	userRepository.delete(id);
        return index(model);
    }
	@RequestMapping(value ="save.do", method = RequestMethod.POST)
	public String save(Model model,UserDto userDto){
        if("ok".equals(checkHaveUser(""+userDto.getId(),userDto.getName()))){
            if(userDto.getId()>0){
               return editUser(model,userDto.getId());
            }
            return ADD;
        }
        userRepository.save(userDto);
        return index(model);
		
	}


    @RequestMapping(value ="login.do" )
    public String login(HttpSession session,UserDto userDto){

    	logger.info("userDto= " + userDto);
        UserDto user = userRepository.getByUserName(userDto.getName());
        if(user!=null&&user.getPassword().equals(userDto.getPassword())){
            session.setAttribute("userinfo",user);
            return "redirect:/index.do";
        }
        return LOGIN;
    }

    @RequestMapping(value ="out.do" )
    public String out(HttpSession session){

        session.invalidate();

        return LOGIN;
    }

    @RequestMapping(value ="{id}/{userName}/checkHaveUser.do" )
    @ResponseBody
    public String checkHaveUser(@PathVariable("id")String id,@PathVariable("userName")String userName){
        UserDto user = userRepository.getByUserName(userName);
        if(user!=null&&user.getId()>0){
            if(Tool.getInt(id)==0){
                return "ok";
            }
            if(Tool.getInt(id)!=user.getId()){
                return "ok";
            }
        }
        return "no";
    }
}
